﻿using System.Collections.Generic;
using System.Windows.Forms;
using HuaCheng.DataAccess;
using System;
using System.Text.RegularExpressions;
using HuaCheng.CommonClass;


namespace HuaCheng.Views
{
    /// <summary>
    /// 系统设置
    /// </summary>
    public partial class SystemSet : Form
    {
        public SystemSet()
        {
            InitializeComponent();
            //加载系统设置
            LoadSystemSet();
        }
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, System.EventArgs e)
        {
            //验证上限必须大于下限
            if (tbDcBusVol.Text == "" || tbMagnification.Text == "" || tbVolMin.Text == "" || tbVolMax.Text == "" || tbCurrMin.Text == "" || tbCurrMax.Text == "" || tbPowerMax.Text=="")
            {
                MessageBox.Show("系统设置均不能为空！");
                tbDcBusVol.Focus();
            }
            else
            { 
                if (Convert.ToInt32(tbVolMin.Text) >= Convert.ToInt32(tbVolMax.Text))
                {
                    MessageBox.Show("电压量程下限必须小于上限！");
                    tbVolMin.Text = "";
                    tbVolMin.Focus();
                }
                else if (Convert.ToInt32(tbCurrMin.Text) >= Convert.ToInt32(tbCurrMax.Text))
                {
                    MessageBox.Show("电流量程下限必须小于上限！");
                    tbCurrMin.Text = "";
                    tbCurrMin.Focus();
                }
                else
                {
                    List<Dicts> systemSetList = new List<Dicts>();
                    //是否带电池测试
                    string strDicCode = string.Empty;
                    //是否记录跳转条件
                    string strJumpRecordCode = string.Empty;
                    //电压最小值
                    Dicts dicVolMin = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "电压最小值",
                        Code = tbVolMin.Text
                    };
                    systemSetList.Add(dicVolMin);
                    //电压最大值
                    Dicts dicVolMax = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "电压最大值",
                        Code = tbVolMax.Text
                    };
                    systemSetList.Add(dicVolMax);
                    //电流最小值
                    Dicts dicCurrMin = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "电流最小值",
                        Code = tbCurrMin.Text
                    };
                    systemSetList.Add(dicCurrMin);
                    //电流最大值
                    Dicts dicCurrMax = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "电流最大值",
                        Code = tbCurrMax.Text
                    };
                    systemSetList.Add(dicCurrMax);
                    //直流母线电压
                    Dicts dicDcBusVol = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "直流母线电压",
                        Code = tbDcBusVol.Text
                    };
                    systemSetList.Add(dicDcBusVol);
                    //倍率
                    Dicts dicMagnification = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "倍率",
                        Code = tbMagnification.Text
                    };
                    systemSetList.Add(dicMagnification);
                    //功率上限
                    Dicts dicPowerMax = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "功率上限",
                        Code = tbPowerMax.Text
                    };
                    systemSetList.Add(dicPowerMax);
                    //带电池测试
                    if (rbtnYes.Checked == true)
                    {
                        strDicCode = "1";
                    }
                    else if (rbtnNo.Checked == true)
                    {
                        strDicCode = "0";
                    }
                    Dicts dicBatteryTest = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "电池测试",
                        Code = strDicCode
                    };
                    systemSetList.Add(dicBatteryTest);
                    //记录跳转条件
                    if (rbtnY.Checked == true)
                    {
                        strJumpRecordCode = "1";
                    }
                    else if (rbtnN.Checked == true)
                    {
                        strJumpRecordCode = "0";
                    }
                    Dicts dicJumpRecord = new Dicts()
                    {
                        Category = "系统设置",
                        Name = "记录跳转条件",
                        Code = strJumpRecordCode
                    };
                    systemSetList.Add(dicJumpRecord);
                    systemSetList.ForEach(o => DicHelper.UpdateRange(o));
                    this.Close();
                }
            }
        }
        /// <summary>
        /// 加载系统设置
        /// </summary>
        private void LoadSystemSet()
        {
            List<Dicts> systemSetList = DicHelper.RetrieveSystemSet();
            for (int i = 0; i < systemSetList.Count; i++)
            {
                switch (systemSetList[i].Name)
                {
                    case "电压最小值":
                        {
                            tbVolMin.Text = systemSetList[i].Code;
                        } break;
                    case "电压最大值":
                        {
                            tbVolMax.Text = systemSetList[i].Code;
                        } break;
                    case "电流最小值":
                        {
                            tbCurrMin.Text = systemSetList[i].Code;
                        } break;
                    case "电流最大值":
                        {
                            tbCurrMax.Text = systemSetList[i].Code;
                        } break;
                    case "直流母线电压":
                        {
                            tbDcBusVol.Text = systemSetList[i].Code;
                        } break;
                    case "倍率":
                        {
                            tbMagnification.Text = systemSetList[i].Code;
                        } break;
                    case "电池测试":
                        {
                            if (systemSetList[i].Code == "1")
                            {
                                rbtnYes.Checked = true;
                            }
                            else
                            {
                                rbtnNo.Checked = true;
                            }
                        } break;
                    case "记录跳转条件":
                        {
                            if (systemSetList[i].Code == "1")
                            {
                                rbtnY.Checked = true;
                            }
                            else
                            {
                                rbtnN.Checked = true;
                            }
                        } break;
                    case "功率上限":
                        {
                            tbPowerMax.Text = systemSetList[i].Code;
                        } break;
                }
            }
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 除倍率外的输入数值校验
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键和负号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
            {
                e.Handled = true;
            }
            //负号只能在第一位输入
            if (e.KeyChar == (char)('-'))
            {
                if (((TextBox)sender).Text != "")
                {
                    e.Handled = true;
                }
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                e.Handled = true;
            }
            //第一位是负号，第二位不能为小数点
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            //第一位是-，第二位是0时，第三位必须为小数点
            if (((TextBox)sender).Text == "-0" && e.KeyChar != (char)('.') && e.KeyChar != 8)
            {
                e.Handled = true;
            }
        }
        private void txtMagnification_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.'))
            {
                e.Handled = true;
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// 验证数字前面的0
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNumber_Leave(object sender, System.EventArgs e)
        {
            if (Convert.ToString(((TextBox)sender).Text).StartsWith("0"))
            {
                if (Regex.Matches(((TextBox)sender).Text, @"0").Count + Regex.Matches(((TextBox)sender).Text, @"\.").Count != ((TextBox)sender).Text.Length)
                {
                    if (!Convert.ToString(((TextBox)sender).Text).StartsWith("0."))
                    {
                        if (Regex.IsMatch(((TextBox)sender).Text, @"^0+\."))
                        {
                            Regex reg = new Regex(@"^0+");
                            ((TextBox)sender).Text = reg.Replace(((TextBox)sender).Text, "0");
                        }
                        else
                        {
                            Regex reg = new Regex(@"^0+");
                            ((TextBox)sender).Text = reg.Replace(((TextBox)sender).Text, "");
                        }
                    }
                }
                else
                {
                    ((TextBox)sender).Text = "0";
                }
            }
        }
        #region 国际化
        private void SystemSet_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("SystemBasicText");
            this.lblBattery.Text = ResourceCulture.GetString("TestWithBatteryText");
            this.rbtnYes.Text = ResourceCulture.GetString("WithBatteryText");
            this.rbtnNo.Text = ResourceCulture.GetString("WithoutBatteryText");
            this.lblJumpRecord.Text = ResourceCulture.GetString("RecordJumpText");
            this.rbtnY.Text = ResourceCulture.GetString("RecordJumpYesText");
            this.rbtnN.Text = ResourceCulture.GetString("RecordJumpNoText");
            this.lblDcBusVol.Text = ResourceCulture.GetString("DCBusVolText");
            this.lblMagnification.Text = ResourceCulture.GetString("MagnificationText");
            this.lblVolRange.Text = ResourceCulture.GetString("VolRangeText");
            this.lblCurrRange.Text = ResourceCulture.GetString("CurrRangeText");
            this.btnConfirm.Text = ResourceCulture.GetString("SureText");
            this.btnCancel.Text = ResourceCulture.GetString("CancelText");
            this.lblPowerMax.Text = ResourceCulture.GetString("Powerupper");
        }
        #endregion
    }
}
